<template>
	<view class="test-page">
		<web-view v-if="showWebview" id="webview_id" :src="src" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	// const SRC = 'http://172.16.26.175:5500/index.html'
	const SRC = 'https://zhatu.deqing.gov.cn/createPdf/index.html'
	export default {
		onLoad(options) {
			console.log(options)
			this.src = `${SRC}?from=dingding&type=${ options.type || '' }`
			this.webViewContext = my.createWebViewContext('webview_id');
		},
		mounted() {
			console.log('传过来的通行证数据: ', this.licenseData)
			console.log('传过来的通行证数据infoApprove: ', this.infoApprove)
			this.showWebview = true
			this.timer = setInterval(() => {
				// 向webview发送信息
				this.webViewContext.postMessage({
					routeText: this.infoApprove.transitRouteText,
					routePath: this.infoApprove.transitRoute,
					transitCompanyInfo: this.infoApprove.transitCompanyInfo || [],
					infoApprove: this.infoApprove,
					licenseData: this.licenseData
				});
			}, 200)
		},
		destroyed() {
			clearInterval(this.timer)
			this.timer = null
		},
		computed: {
			...mapState('mdApprove', ['infoApprove', 'licenseData']),
		},
		data() {
			return {
				src: '',
				webViewContext: null,
				showWebview: false,
				timer: null,
			}
		},
		methods: {
			...mapMutations('mdOrder', []),
			// webview向app发送信息
			handleMessage({
				detail
			}) {
				console.log('收到webview信息', detail)
				if (!(typeof(detail.data) == "object")) {
					detail.data = JSON.parse(detail.data)
				}
				uni.$emit('busSetRouteTxzPdf', detail.data)
				if (detail.closeMap) {
					uni.navigateBack()
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.test-page {
		width: 100%;
		height: 100%;

		#webview_id {
			width: 100%;
			height: 100%;
		}
	}
</style>
